package com.zibo.convalesce.modules.account.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zibo.convalesce.common.result.AjaxMapResult;
import com.zibo.convalesce.common.result.AjaxResult;
import com.zibo.convalesce.modules.account.pojo.AccountResp;
import com.zibo.convalesce.modules.account.pojo.UserInfoResp;
import com.zibo.convalesce.modules.account.service.AccountService;
import com.zibo.convalesce.modules.account.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zibo.convalesce.common.result.AjaxMapResult.*;

/**
 * @author lx
 * 账户管理
 */

@RestController
@RequestMapping("/account")
public class AccountController {

    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 管理员账号列表查询
     *
     * @return
     */
    @RequestMapping("/adminList")
    public AjaxMapResult adminList(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                                   @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                                   @RequestParam(defaultValue = "", value = "loginName") String user
    ) {
        logger.info("账户管理【管理员账号】入参user==>{}", user);
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum, pageSize);

        if (user == null || "".equals(user)) {
            map.put("user", "");
            // 查询管理员账号所有数据，
            List<AccountResp> admins = accountService.listAll(map);
            admins.forEach(val -> {
                if ("0".equals(val.getSex())) {
                    val.setSex("男");
                } else if ("1".equals(val.getSex())) {
                    val.setSex("女");
                } else {
                    val.setSex("其他");
                }
                if ("all".equals(val.getCourtyard())) {
                    val.setCourtyard("全部院区");
                } else if ("mk".equals(val.getCourtyard())) {
                    val.setCourtyard("民康院区");
                } else if ("yml".equals(val.getCourtyard())) {
                    val.setCourtyard("一马路院区");
                } else {
                    val.setCourtyard("其他院区");
                }
            });
            PageInfo<AccountResp> page = new PageInfo<>(admins);
            return successData(page.getTotal(), admins);
        }

        map.put("user", user);
        List<AccountResp> admins = accountService.listAll(map);
        admins.forEach(val -> {
            if ("0".equals(val.getSex())) {
                val.setSex("男");
            } else if ("1".equals(val.getSex())) {
                val.setSex("女");
            } else {
                val.setSex("其他");
            }
            if ("all".equals(val.getCourtyard())) {
                val.setCourtyard("全部院区");
            } else if ("mk".equals(val.getCourtyard())) {
                val.setCourtyard("民康院区");
            } else if ("yml".equals(val.getCourtyard())) {
                val.setCourtyard("一马路院区");
            } else {
                val.setCourtyard("其他院区");
            }
        });
        PageInfo<AccountResp> page = new PageInfo<>(admins);
        return successData(page.getTotal(), admins);
    }

    @RequestMapping("/userList")
    public AjaxMapResult userList(@RequestParam(defaultValue = "1", value = "page") Integer pageNum,
                               @RequestParam(defaultValue = "10", value = "limit") Integer pageSize,
                               @RequestParam(defaultValue = "", value = "adminId") String user
    ) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum, pageSize);
        if (user == null || user.equals("")) {
            map.put("user", "");
            List<UserInfoResp> userInfos = userInfoService.userListAll(map);
            PageInfo<UserInfoResp> page = new PageInfo<>(userInfos);
            return successData(page.getTotal(), userInfos);
        }
        map.put("user", user);
        List<UserInfoResp> userInfos = userInfoService.userListAll(map);
        PageInfo<UserInfoResp> page = new PageInfo<>(userInfos);
        return successData(page.getTotal(), userInfos);
    }


}
